package com.sgy.utils;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.sqlite.JDBC;

import javax.sql.DataSource;

/**
 * @author shenguangyang
 * @date 2022-08-14 8:05
 */
public class SqliteBuilder {
    private String filePath;

    private String url;

    public static SqliteBuilder create() {
        return new SqliteBuilder();
    }

    public SqliteBuilder filePath(String filePath) {
        this.filePath = filePath;
        return this;
    }

    public SqliteBuilder url(String url) {
        this.url = url;
        return this;
    }

    public DataSource build() {
        if (url != null && !url.equals("")) {
            return DataSourceBuilder.create().
                    url(url).driverClassName(JDBC.class.getName()).build();
        }
        if (filePath != null && !filePath.equals("")) {
            String stringBuilder = "jdbc:sqlite:" + filePath;
            return DataSourceBuilder.create().
                    url(stringBuilder).driverClassName(JDBC.class.getName()).build();
        }
        return DataSourceBuilder.create().build();
    }
}
